package ace.tool.generate.code.impl.resolver.ui.resolver;

import ace.tool.generate.code.impl.resolver.ui.model.bo.UiProjectCommonInfo;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import lombok.Builder;
import org.apache.commons.text.WordUtils;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@Builder
public class UiProjectCommonInfoResolver {
  private GenerateInput input;

  public UiProjectCommonInfo resolve() {
    return UiProjectCommonInfo.builder()
        .projectModuleName(this.getProjectModuleName())
        .projectModuleCodeName(this.getProjectModuleCodeName())
        .rootDir(this.getProjectRootDir())
        .srcDir(this.getProjectRootDir() + "/src")
        .coreDir(this.getCoreDir())
        .coreModuleDir(this.getCoreModuleDir())
        .mockModuleDir(this.getMockModuleDir())
        .build();
  }

  private String getProjectModuleCodeName() {
    boolean hasDash = this.input.getProjectName().contains("-");
    if (hasDash) {
      StringBuilder sb = new StringBuilder();
      String[] projectNames = this.input.getProjectName().split("-");
      for (int i = 0; i < projectNames.length; i++) {
        if (i == 0) {
          sb.append(WordUtils.uncapitalize(projectNames[i]));
        } else {
          sb.append(WordUtils.capitalize(projectNames[i]));
        }
      }
      return sb.toString();
    }

    return this.input.getProjectName();
  }

  private String getMockModuleDir() {
    return this.getProjectRootDir()
        + this.input.getUiMockDirRelativePath()
        + "/"
        + this.getProjectModuleName();
  }

  private String getProjectModuleName() {
    return this.input.getProjectName();
  }

  private String getCoreModuleDir() {
    return this.getCoreDir() + "/" + this.getProjectModuleName();
  }

  private String getCoreDir() {
    return this.getProjectRootDir() + this.input.getUiCoreDirRelativePath();
  }

  private String getProjectRootDir() {
    return input.getProjectRootDir() + "/" + input.getUiProjectName();
  }
}
